package com.wangan.wanganpush.services;

import com.wangan.wanganpush.dao.VehicleDao;
import com.wangan.wanganpush.model.Vehicle;
import com.wangan.wanganpush.utils.FileNameParse;
import com.wangan.wanganpush.xml.VehicleXml;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class VehicleImpl extends BaseImpl{
    @Autowired
    VehicleDao vehicleDao;

    @Autowired
    GetXmlSendZipUtil getXmlSendZipUtil;

    @Autowired
    VehicleXml vehicleXml;

    @Autowired
    FileNameParse fileNameParse;

    public Boolean vehiclePush() {
        try {
            //查询出需要推送的数据
            List<Vehicle> li = vehicleDao.findAllByFlag(0);

            //调用推送方法
            if (li != null && li.size() > 0) {
                //拿取消息流水号
                String messageCode = fileNameParse.getMessageCode();
                //拿取文件名
                String fileName =fileNameParse.parseFileName("VEHICLEINFO","3",messageCode);

                //把数据转xml字符串
                String str = vehicleXml.parseXml(li,messageCode,fileName);

                //送往xml转zip包的流程并推送网安
                getXmlSendZipUtil.sendZip("VEHICLEINFO",str,messageCode,fileName,null);

            }
            System.out.println(li.size());

            li = super.updateFlag(li);

            //更改数据库flag字段为1已经发送
            if (li != null && li.size() > 0) {
                //更新数据
                vehicleDao.save(li);
                //提交事务
                vehicleDao.flush();
            }

            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }
}
